package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/11/4 16:35
 * @question:
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired  //缓存验证码
    private RedisTemplate redisTemplate;

    /**
     * 用户获取验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {

        //前端页面获取用户手机号
        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)) {
            //获取验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
//            //存入session域中
//            session.setAttribute(phone,code);
            //将获得的验证码存入redis key为手机
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            log.info("code = {}",code);

            return R.success("验证码发送成功");
        }
        return R.error("短信发送失败");
    }

    /**
     * 用户登录 需要获取验证码和手机号信息 所以直接使用map集合即可
     * 不需要再创建vo对象 就两个值
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> loginR(@RequestBody Map map, HttpSession session) {

        //获取前端传来的手机号和验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();

//        //获取出来提前存入的验证码
//        Object codeInSession = session.getAttribute(phone);
        //获取之前缓存的验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);
        //对验证码进行一个判断
        if (codeInSession != null && codeInSession.equals(code)) {
            //返回成功信息
            //如果改手机号是一个新用户,则自动帮忙注册
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone,phone);
            User user = userService.getOne(wrapper);
            if (user == null) {
                //自动注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            //登陆成功 将用户id放入session一份 否则拦截器会拦截掉请求 不让登录
            session.setAttribute("user",user.getId());
            //登陆成功 记着将验证码删除
            redisTemplate.delete(phone);
            return R.success(user);

        }
        //否则,登陆失败
        return R.error("登陆失败");
    }

    /**
     * 用户退出登录
     * 请求 URL: http://localhost:8080/user/loginout
     * 请求方法: POST
     * @param session
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginOut(HttpSession session) {
        //直接清掉存储的id即可
        session.invalidate();

        return R.success("用户退出登陆成功~");
    }
















}
